#! /bin/sh

#---------------------------------------------------------------------
#                                init                                 
# 
# This is called by the template init method, which is itself called
# by fw-init when a project is being initialized.  It is intended
# for creating files in the project that will remain in the project
# and are owned by the project.  Creation of (symlinks to) files
# owned by the build method should be done at bootstrap time.
#---------------------------------------------------------------------

#---------------------------------------------------------------------
#                           chicken and egg                           
#---------------------------------------------------------------------

which perl >/dev/null 2>/dev/null || {
  echo "erlang-rebar/init: fatal: can't locate perl" 1>&2
  exit 1
}

if test -z "${FW_ROOT}"
  then
    FW_ROOT=`echo "$0" | perl -pe 's%fw/template/erlang-rebar/init$%%;'`
    export FW_ROOT
  fi

test -f "${FW_ROOT}/share/fw/sh/fw-find.sh" || {
  echo "erlang-rebar/init: fatal: can't determine FW_ROOT" 1>&2
  echo "erlang-rebar/init: please set the environment variable" 1>&2
  exit 1
}

. "${FW_ROOT}/share/fw/sh/fw-find.sh"

#---------------------------------------------------------------------
#                              routines                               
#---------------------------------------------------------------------

fw_source "erlang-rebar/init" "sh/fatal.sh"
fw_source "erlang-rebar/init" "sh/fw-exec.sh"
fw_source "erlang-rebar/init" "sh/parse-args.sh"
fw_source "erlang-rebar/init" "sh/validate-args.sh"

#---------------------------------------------------------------------
#                                main                                 
#---------------------------------------------------------------------

fw_exec "build/automake/init" "$@" || fatal "erlang-rebar/init"

parse_args "erlang-rebar/init" "$@"

validate_args "erlang-rebar/init" "" ""

if test -n "${wrap_git_path}" ; then
  # derive a name from the path as a default
  git_name="`echo "${wrap_git_path}" | perl -ne 'print $2 if m#(git|http|https)://.*/([^/.]*).git#'`-origin"
  wrap_git_name=${wrap_git_name:-$git_name}
fi

if test -z "${wrap_git_path}" ; then
  for dir in .
    do
      for x in rebar.config
        do
          fw_find "template/erlang-rebar/$dir/$x" file
          test -z "$file" && fatal "erlang-rebar/init"
          cp -f "$file" "$dir" || fatal "erlang-rebar/init"
          chmod +w "$dir/$x" || fatal "erlang-rebar/init"
          fw_exec "revision/$revision/add-files" $x
        done
    done

  for dir in src
    do
      mkdir "$dir" || fatal "erlang-rebar/init"
      fw_exec "revision/$revision/add-dir" "$dir" || fatal "erlang-rebar/init"

      for x in myapp.erl
        do
          fw_find "template/erlang-rebar/$dir/$x" file
          test -z "$file" || {
            cp -f "$file" "$dir" || fatal "erlang-rebar/init"
            chmod +w "$dir/$x" || fatal "erlang-rebar/init"
          }
        done
      (
        cd "$dir" &&                                              \
        fw_exec "revision/$revision/add-files"                    \
        myapp.erl
      ) || fatal "erlang-rebar/init"
    done
else
  fw_exec "revision/$revision/ignore-files" \
    "${wrap_git_name}" \
    c_src \
    ebin \
    include \
    priv \
    src \
    "test" \
    "rebar.config" \
  || fatal "erlang-rebar/init"
fi

fw_exec "revision/$revision/ignore-files" \
  rebar \
  .generated_app_file \
  .eunit

(
  cd fw-pkgin &&
  perl -i -pe 's/FW_PACKAGE_ARCHITECTURE_DEPENDENT="1"/FW_PACKAGE_ARCHITECTURE_DEPENDENT="0"/' config
) || fatal "erlang-rebar/init"

(
  cd fw-pkgin &&
  cat <<'EOD' >> config

# uncomment and set manually if you want the application name to be different
# from FW_PACKAGE_NAME, application name must consist of characters [a-zA-Z_]
# FW_ERL_APP_NAME=""

# uncomment and set manually if autodetection of modules is incorrect.
# should be an erlang expression which evaluates to a list.
# FW_ERL_APP_MODULES="[]"

# uncomment and set manually if autodetection of registered processes is incorrect
# should be an erlang expression which evaluates to a list.
# FW_ERL_APP_REGISTERED="[]"

# uncomment and set manually if autodetection of start module is incorrect
# should be an erlang expression which evaluates to an atom.
# FW_ERL_APP_START_MODULE=""

# uncomment to define start args to the start module.  should be an erlang
# expression which evaluates to a list.  this only has an effect if
# FW_ERL_APP_START_MODULE is set manually.
# FW_ERL_APP_START_ARGS="[]"

# uncomment if the module line being generated is incorrect and you want 
# to override it.
# FW_ERL_APP_MOD_LINE="{ mod, { $FW_ERL_APP_START_MODULE, $FW_ERL_APP_START_ARGS } }"

# uncomment to define the application environment variables. should be an 
# erlang expression which evaluates to a list.
# FW_ERL_APP_ENVIRONMENT="[]"

# uncomment to indicate additional OTP applications (besides kernel and stdlib)
# that this application depends upon.  should be an erlang expression which 
# evaluates to a list.
# FW_ERL_PREREQ_APPLICATIONS_EXTRA="[]"

# uncomment to add arbitrary extra content to the app file, e.g., an
# included application directive.  should be an erlang expression which 
# evaluates to a proplist (list of key-value tuple pairs).
# FW_ERL_APP_EXTRA="[]"

# uncomment and set to specify additional pkg-config packages on the Requires:
# line of the generated .pc file
# FW_PKGCONFIG_REQUIRES_EXTRA=""

# uncomment and set to specify additional content for the Libs:
# line of the generated .pc file
# FW_PKGCONFIG_LIBS_EXTRA=""

# uncomment and set to specify additional content for the Cflags:
# line of the generated .pc file
# FW_PKGCONFIG_CFLAGS_EXTRA=""

# uncomment and set to add arbitrary additional content to the.
# generated .pc file
# FW_PKGCONFIG_EXTRA=""
EOD

if test -n "${wrap_git_path}" ; then
  echo "" >> config
  echo "# upstream git repo to package via framewerk" >> config
  echo "FW_WRAP_GITPATH=\"${wrap_git_path}\"" >> config
  echo "" >> config
  echo "# local name for git repo clone" >> config
  echo "FW_WRAP_GITNAME=\"${wrap_git_name}\"" >> config
  echo "" >> config
  echo "# git tag to checkout" >> config
  echo "FW_WRAP_GITTAG=\"${wrap_git_tag}\"" >> config
fi

) || fatal "erlang-rebar/init"
